package com.github.jiangxch.rpc.transport.exchanger.jdk;

import com.github.jiangxch.rpc.transport.Channel;
import com.github.jiangxch.rpc.transport.exchanger.ExchangeChannel;
import com.github.jiangxch.rpc.transport.exchanger.Request;
import com.github.jiangxch.rpc.transport.exchanger.UuidUtil;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;

/**
 * @author: jiangxch
 * @date: 2021/2/26 上午12:55
 */
public class JdkExchangeChannel implements ExchangeChannel {
    private Channel channel;
    
    public JdkExchangeChannel(Channel channel) {
        this.channel = channel;
    }
    
    @Override
    public CompletableFuture<Object> request(Object message) throws IOException {
        Request request = new Request();
        request.setRequestId(UuidUtil.getUuid());
        request.setData(message);
        send(request);
        return FutureManager.newRequestFuture(request);
    }
    
    @Override
    public InetSocketAddress getRemoteAddress() {
        return channel.getRemoteAddress();
    }
    
    @Override
    public void send(Object message) throws IOException {
        channel.send(message);
    }
}
